package com.example.bookstore.utils;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.exceptions.JedisConnectionException;

import java.util.ResourceBundle;

public class RedisProvider {

    private static JedisPool jedispool;

    static {
        ResourceBundle bundle = ResourceBundle.getBundle("redis");  //读取redis.properties文件
        if (bundle == null) {
            throw new IllegalArgumentException(
                    "[redis.properties] is not found!");
        }
        try {
            JedisPoolConfig jedisconfig = new JedisPoolConfig();
            jedisconfig.setMaxIdle(Integer.valueOf(bundle
                    .getString("redis.pool.maxIdle")));
            jedisconfig.setTestOnBorrow(Boolean.valueOf(bundle
                    .getString("redis.pool.testOnBorrow")));
            jedisconfig.setTestOnReturn(Boolean.valueOf(bundle
                    .getString("redis.pool.testOnReturn")));
            jedispool = new JedisPool(jedisconfig, bundle.getString("redis.ip"),
                    Integer.valueOf(bundle.getString("redis.port")),
                    Integer.valueOf(bundle.getString("redis.timeout")),
                    bundle.getString("redis.password"),
                    Integer.valueOf(bundle.getString("redis.database")));
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public  Jedis getJedis() {
        Jedis jedis = null;
        try {
            jedis = jedispool.getResource();
            System.out.println("22222222222222");
        } catch (JedisConnectionException jce) {
            jce.printStackTrace();
        } finally {
            assert jedis != null;
            jedis.close();
        }
        return jedis;
    }


}